package org.proddevplm.business.diagram;

import java.util.Map;
import java.util.Set;

import org.proddevplm.business.api.AController;
import org.proddevplm.business.diagram.constants.DiagramConstans;
import org.proddevplm.constants.ControllerConstants;
import org.proddevplm.constants.UserConstants;
import org.proddevplm.dao.data.User;
import org.proddevplm.dao.data.activities.Activity;
import org.proddevplm.dao.data.dia.Diagram;
import org.proddevplm.dao.data.diagram.DiagramEntity;
import org.proddevplm.dao.data.diagram.EntityStructure;
import org.proddevplm.helpers.DiagramEntityBuilder;

public class DiagramController extends AController{

	@Override
	public Object execute(int command, Object data) {

		Map<String, Object> diaMap = (Map<String, Object>) data;
        int productId = (Integer) getSession().getAttribute("productId");
        User user = (User) getSession().getAttribute(UserConstants.USER);
        
        switch (command) {
        
        case DiagramConstans.SAVE_DIA:
        	
        	try{
        		Diagram dia = new Diagram();
        		dia.setProductId(productId);
        		dia.setDiaEncoding((String)diaMap.get("diaEncoding"));
        		
        		getDaoManager().getDiaManager().saveDia(dia);
        		
        		EntityStructure structure = DiagramEntityBuilder.getEntityStructure((String)diaMap.get("diaEncoding"));
        		Map<String, DiagramEntity> entityMap = structure.getEntityMap();
        		for(String key : entityMap.keySet()){
        			DiagramEntity entity = entityMap.get(key);
        			if((Integer.valueOf(entity.getType())==1)||(Integer.valueOf(entity.getType())==2)){
        				Activity activity = new Activity();
        				activity.setName(entity.getActivityName());
        				activity.setResponsible(entity.getActivityResponsible());
        				activity.setApprover(entity.getActivityAppriver());
        				activity.setProductId(productId);
        				activity.setStartDate(entity.getActivityStartDate());
        				activity.setEndDate(entity.getActivityEndDate());
        				activity.setToolName(entity.getToolName());
        				activity.setToolControlString(entity.getToolControlString());
        				activity.setActivityLifeCycle("In work");
        				
        				Set<String> previous = entity.getPrevious();
        				for(String s:previous){
        					DiagramEntity elementBefore = structure.getEntity(s);
        					if((Integer.valueOf(elementBefore.getType())==3)||(Integer.valueOf(elementBefore.getType())==4)){
        						activity.setFilePath(elementBefore.getFilePath());
        						continue;
        					}
        				}
        				/*
        				String[] previousElements = (String[]) previous.toArray();
        				for(int j=0; j<previousElements.length; j++){
        					DiagramEntity elementBefore = structure.getEntity(previousElements[j]);
        					if((Integer.valueOf(elementBefore.getType())==3)||(Integer.valueOf(elementBefore.getType())==4)){
        						activity.setFilePath(elementBefore.getFilePath());
        						continue;
        					}
        				}*/
        				
        				getDaoManager().getActivityManager().addActivity(activity);
        			}
        			
        		}
        		
        	}catch(Exception e){
        		e.printStackTrace();
        	}
        	
        	putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab3");
            break;
            
        case DiagramConstans.LOAD_DIA:
        	Diagram loadedDia = getDaoManager().getDiaManager().loadDia(productId);
        	putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab3");
           	if(loadedDia!=null){
        		return loadedDia.getDiaEncoding();
        	}else return 1;
        	
        case DiagramConstans.UPDATE_DIA:
        	Diagram currentDia = getDaoManager().getDiaManager().loadDia(productId);
        	try{
        		currentDia.setDiaEncoding((String)diaMap.get("diaEncoding"));
        		getDaoManager().getDiaManager().updateDia(currentDia);
        		
        		//Diagram test = getDaoManager().getDiaManager().loadDia(productId);
        		//EntityStructure structure = DiagramEntityBuilder.getEntityStructure(test.getDiaEncoding());
        		EntityStructure structure = DiagramEntityBuilder.getEntityStructure((String)diaMap.get("diaEncoding"));
        		Map<String, DiagramEntity> entityMap = structure.getEntityMap();
        		for(String key : entityMap.keySet()){
        			DiagramEntity entity = entityMap.get(key);
        			if((Integer.valueOf(entity.getType())==1)||(Integer.valueOf(entity.getType())==2)){
        				
        				Activity existingActivity = getDaoManager().getActivityManager().getActivityByNameandProductId(entity.getActivityName(), productId);
        				if(existingActivity == null){
	        				Activity activity = new Activity();
	        				activity.setName(entity.getActivityName());
	        				activity.setResponsible(entity.getActivityResponsible());
	        				activity.setApprover(entity.getActivityAppriver());
	        				activity.setProductId(productId);
	        				activity.setStartDate(entity.getActivityStartDate());
	        				activity.setEndDate(entity.getActivityEndDate());
	        				activity.setToolName(entity.getToolName());
	        				activity.setToolControlString(entity.getToolControlString());
	        				activity.setActivityLifeCycle("In work");
	        				
	        				Set<String> previous = entity.getPrevious();
	        				for(String s:previous){
	        					DiagramEntity elementBefore = structure.getEntity(s);
	        					if((Integer.valueOf(elementBefore.getType())==3)||(Integer.valueOf(elementBefore.getType())==4)){
	        						activity.setFilePath(elementBefore.getFilePath());
	        						continue;
	        					}
	        				}
	        				
	        				getDaoManager().getActivityManager().addActivity(activity);
        				}
        				else{
        					existingActivity.setName(entity.getActivityName());
        					existingActivity.setResponsible(entity.getActivityResponsible());
        					existingActivity.setApprover(entity.getActivityAppriver());
        					existingActivity.setProductId(productId);
        					existingActivity.setStartDate(entity.getActivityStartDate());
        					existingActivity.setEndDate(entity.getActivityEndDate());
        					existingActivity.setToolName(entity.getToolName());
        					existingActivity.setToolControlString(entity.getToolControlString());
        					
        					Set<String> previous = entity.getPrevious();
            				for(String s:previous){
            					DiagramEntity elementBefore = structure.getEntity(s);
            					if((Integer.valueOf(elementBefore.getType())==3)||(Integer.valueOf(elementBefore.getType())==4)){
            						existingActivity.setFilePath(elementBefore.getFilePath());
            						continue;
            					}
            				}
        					
        					getDaoManager().getActivityManager().updateActivity(existingActivity);
        				}

        			}
        			
        		}
        	}catch(Exception e){
        		e.printStackTrace();
        	}
        	
        	putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab3");
            break;
        }
	
		return null;
	}

}